-- NewsPaperRender33
-- 对应样式 3*3

local AvatarFactory = require("app.public.avatar.AvatarFactory")
local UserBaseInfo = require("app.public.dao.UserBaseInfo")

local ClassRef = app.mvc.createRenderClass(...)

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewsPaperRender_33))
    sprite:init()
    return sprite
end

function ClassRef:init(data)
    self.super.init(self, data)

    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        local data = self._data
        local eventId = data.eventId
        local playerInfo = data.newsInfo or {}
        local extraStr = (playerInfo[1] and playerInfo[1].BaseInfo) and playerInfo[1].BaseInfo.name or "" -- 目前只需要玩家名字
        local conf = GD:getNewsPaperEventById(eventId)
        if not conf then return end
        
        if conf.pic33 then
            local bgPath = string.format("%s%s.png",Res.newsPaperPath,conf.pic33)
            self._ui.bg:loadTexture(bgPath)
        end
        local tltStr1 = conf.title1 or ""
        local tltStr2 = conf.title2 or ""
        if conf.title2 then -- 有副标题的话，主副标题换一下位置
            self._ui.tlt_1:setString(string.format(L(tltStr2), extraStr))
            self._ui.tlt_2:setString(string.format(L(tltStr1), extraStr)) 
        else
            self._ui.tlt_1:setString(string.format(L(tltStr1), extraStr))
            self._ui.tlt_2:setString("")
        end
        
        local params = playerInfo[1] and  UserBaseInfo.new(playerInfo[1].BaseInfo)
        if params and tonumber(conf.player) == 1 then
            params.mountID = 0
            local heroNode = self._ui.Node_position
            local nodeSize = heroNode:getContentSize()
            self:performWithDelay(function()
                params.inBattle = false
                local hero = AvatarFactory.createHero(params)
                hero:setTag(0)
                hero:setScale(0.65)
                hero:setPositionX(nodeSize.width/2)
                hero:playAni({name="mapidle",next="mapidle"})
                heroNode:addChild(hero)
            end, 0.2)
            heroNode:setTouchEnabled(true)
            heroNode:addTouchEventListener(function(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    if params.uid ~= UD.player.uid then --点击别人的头像
                        local size = sender:getContentSize()
                        local pos = sender:convertToWorldSpace(cc.p(size.width, size.height/2))
                        UIHandler:showHeadMenu({online=true, attr=params, pos=pos})
                    end
                end
            end)
        end
    end
end

return ClassRef